package io.xccit.race.service.impl;

import io.minio.messages.Bucket;
import io.minio.messages.Item;
import io.xccit.common.utils.MinIOUtils;
import io.xccit.race.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class MinioServiceImpl implements MinioService {

    @Autowired
    private MinIOUtils minIOUtils;

    @Override
    public String upload(MultipartFile file) throws Exception {
        return minIOUtils.upload(file);
    }

    @Override
    public List<String> uploadBatch(List<MultipartFile> files) throws Exception {
        List<String> urls = new ArrayList<>();
        for (MultipartFile file : files) {
            urls.add(minIOUtils.upload(file));
        }
        return urls;
    }

    @Override
    public String uploadToBucket(String bucket, MultipartFile file) throws Exception {
        minIOUtils.putObject(bucket, file.getOriginalFilename(), file);
        return minIOUtils.getObjectUrl(bucket, file.getOriginalFilename());
    }

    @Override
    public List<Bucket> listBuckets() throws Exception {
        return minIOUtils.listBuckets();
    }

    @Override
    public void createBucket(String bucket) throws Exception {
        if (!minIOUtils.existBucket(bucket)) {
            minIOUtils.makeBucket(bucket);
        }
    }

    @Override
    public void deleteBucket(String bucket) throws Exception {
        minIOUtils.removeBucket(bucket);
    }

    @Override
    public List<Item> listObjects(String bucket, String prefix, boolean recursive) throws Exception {
        return minIOUtils.listObjects(bucket, prefix, recursive);
    }

    @Override
    public boolean deleteObject(String bucket, String object) throws Exception {
        return minIOUtils.removeObject(bucket, object);
    }

    @Override
    public String getPresignedUrl(String bucket, String object, int expires) throws Exception {
        return minIOUtils.getPresignedUrl(bucket, object, expires);
    }
}


